# GF_PY3_CLASS_Finance_Indicator_EMA_s_SQLITE3_s_BR.py
# Create By GF 2024-01-22 18:16

import sqlite3  # Import Validation: SQLite3 2.6.0 from Python 3.12.0 / Requires SQLite 3.7.15 or Newer
# ..................................................
import GF_PY3_CLASS_Finance_Indicator_EMA_s_Breakpoint_Recovery

# ##################################################

class Finance_Indicator_EMA_s_SQLITE3_s_BR(GF_PY3_CLASS_Finance_Indicator_EMA_s_Breakpoint_Recovery.Finance_Indicator_EMA_s_Breakpoint_Recovery):

    def __init__(self, SQLITE3_CONNECT, DB_TABLE:str):

        # 注意:
        # 需要在函数外创建 SQLite 3 数据库连接 (以便于复用及提高代码效率)
        # 示例:
        # >>> SQLITE3_CONNECT = sqlite3.connect("./EXAMPLE.db")  # 连接到 SQLite 3 数据库 (如果文件不存在, 会自动创建)
        # >>> SQLITE3_CONNECT.commit()                           # SQLIT3 提交事务 (查询无需 Commit, 删改需要 Commit 使事务生效)
        # >>> SQLITE3_CONNECT.close()                            # SQLIT3 关闭 Connection

        self.SQLITE3_CONNECT = SQLITE3_CONNECT
        self.DB_TABLE:str = DB_TABLE

        # ##########################################################################
        # "父类/基类" 初始化变量 ("子类/派生类" 会覆盖 "父类/基类" 的 __init__ 方法)
        # ##########################################################################

        self.JSON_Digit_of_Previous_EMA:float = None

    def UPDATE_OR_INSERT_EMA_s_BR(self, Table:str, ID:int, Time:str, Code:str, ROW_NUM:int, MAX_ROW_NUM:int, Period:int, Close:float) -> dict:

        Affected_Numbers = 0
        # ..........................................
        Cursor = self.SQLITE3_CONNECT.cursor()  # 创建一个 Cursor 对象并调用其 execute() 方法执行 SQL 命令
        # ..........................................
        EMA_Value:float = self.EMA_s_Breakpoint_Recovery(Table = Table, Time = Time, Code = Code, ROW_NUM = ROW_NUM, MAX_ROW_NUM = MAX_ROW_NUM, Period = Period, Close = Close)
        # ..........................................
        # Possible Errors: sqlite3.OperationalError: no such column: None
        # SQLite 3 does not Support None, NULL should be Used Instead.
        EMA_Value = "NULL" if (EMA_Value == None) else round(EMA_Value, 4)
        # ..........................................
        Cursor.execute(f"SELECT COUNT(*) AS ID_COUNT FROM {self.DB_TABLE} WHERE id = {ID}")
        Queried = Cursor.fetchone()  # 获取查询到的第 1 个结果
        ID_COUNT = Queried[0]        # 获取查询结果第 1 个值
        # ..........................................
        if (ID_COUNT == 0):
            Cursor.execute(f"INSERT INTO {self.DB_TABLE} (id, ema{Period}) VALUES ({ID}, {EMA_Value});")
            Affected_Numbers = Cursor.rowcount  # 获取受影响的行数
        if (ID_COUNT >= 1):
            Cursor.execute(f"UPDATE {self.DB_TABLE} SET ema{Period} = {EMA_Value} WHERE id = {ID};")
            Affected_Numbers = Cursor.rowcount  # 获取受影响的行数
        # ..........................................
        Cursor.close()  # SQLIT3 关闭 Cursor
        # ..........................................
        return {"affected_numbers": Affected_Numbers, "value": EMA_Value}

# EOF Signed by GF.
